<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Http\Controller\Test;

use Swoft\Http\Message\Request;
use Swoft\Http\Message\Cookie;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;

/**
 * Class SwoftCookieController
 *
 * @since 2.0
 *
 * @Controller(prefix="Cookie")
 */
class SwoftCookieController
{
    /**
     * @RequestMapping(route="set",method={RequestMethod::GET})
     *
     * @return Response
     */
    public function set()
    {
        $cookie = new Response();
        $cookies = [
            'key1' => 'value1',
            'key2' => [
                'value' => 'value2',
            ],
            'key3' => [
                'value'    => 'value3',
                'httpOnly' => true
            ],
        ];
        $cookie->setCookies($cookies);
        return '设置Cookie';
    }

    /**
     * @RequestMapping(route="get",method={RequestMethod::GET})
     *
     * @param Request $request
     *
     * @return array
     */
    public function get()
    {
        $cookie = new Response();
        $ck1 = $cookie->getCookie('key1');
        $cks = $cookie->getCookies();
        $key1 = $cookie->hasCookie('key1');
        $not_exist = $cookie->hasCookie('not-exist');
        return  [$ck1,$cks,$key1,$not_exist];
    }

    /**
     * @RequestMapping(route="del",method={RequestMethod::GET})
     *
     * @return Response
     */
    public function del()
    {
        $cookie = new Response();
        $cookie->delCookie('key1');
        return '删除Cookie';
    }
}
